package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Posts;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.PostsService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Posts)表控制层
 *
 * @author makejava
 * @since 2021-05-06 21:44:41
 */
@Controller
@RequestMapping("posts")
public class PostsController {
    /**
     * 服务对象
     */
    @Resource
    private PostsService postsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.postsService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toPosts")
    public String toPosts() {
        return "back/posts";
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toUserPosts")
    public String toUserPosts() {
        return "back/userPosts";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Posts posts) {
        PageInfo pageInfo = postsService.queryAll(page, limit, posts);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryMyPosts")
    public Result queryMyPosts(Integer page, Integer limit, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User userInfo = (User)session.getAttribute("userInfo");
        int userId = userInfo.getUserId();
        PageInfo pageInfo = postsService.queryMyPosts(page, limit, userId);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     * @param posts 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Posts posts) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.postsService.insert(posts));
    }

    /**
     * 修改数据
     * @param posts 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Posts posts) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.postsService.update(posts));
    }

    /**
     * 通过主键删除数据
     *
     * @param postsId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer postsId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.postsService.deleteById(postsId));
    }
}
